	<?php

namespace app\controllers;

use yii\web\Controller;

class AnlianController extends Controller {
	public $user_id = 'BA100162_01'; // AC000039/BA100162_01
	public $password = 'Z43Yz!b2'; // password/Z43Yz!b2
	public $api_server = 'https://eservice.allianz.cn/IG_ePolicy/OTI-azcn-policy'; // test/eservice
	public $password_server = 'https://eservice.allianz.cn/IG_ePolicy/EncryptPassword'; // test/eservice
	public $api_method = 'post';

	function curlPost($url, $data) {
		$ch = curl_init ();
		
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_PORT, 443 );
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );
		
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );
		
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
		
		curl_setopt ( $ch, CURLOPT_POST, true );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
		
		$ret = curl_exec ( $ch );
		$err = curl_error ( $ch );
		
		curl_close ( $ch );
		return ( string ) $ret;
	}
	
	// 获取时间戳
	public function getTimeStamp() {
		list ( $msec, $sec ) = explode ( ' ', microtime () );
		return ( float ) sprintf ( '%.0f', (floatval ( $msec ) + floatval ( $sec )) * 1000 );
	}
	
	// 获取XML头
	public function getXML($type) {
		$xml = new \DOMDocument ( '1.0', 'utf-8' );
		
		$packet = $xml->createElement ( 'Packet' );
		$xml->appendChild ( $packet );
		$packet->setAttribute ( 'type', 'REQUEST' );
		$packet->setAttribute ( 'version', '1.0' );
		
		$head = $xml->createElement ( 'Head' );
		$packet->appendChild ( $head );
		
		$body = $xml->createElement ( 'Body' );
		$packet->appendChild ( $body );
		
		$request_id = $xml->createElement ( 'RequestID', $this->user_id . $type . $this->getTimeStamp ( $type ) );
		$head->appendChild ( $request_id );
		
		$request_type = $xml->createElement ( 'RequestType', $type );
		$head->appendChild ( $request_type );
		
		$request_user = $xml->createElement ( 'User', $this->user_id );
		$head->appendChild ( $request_user );
		
		$request_password = $xml->createElement ( 'Password', $this->getEncryptPassword () );
		$head->appendChild ( $request_password );
		
		return $xml;
	}
	//
	public function getEncryptPassword() {
		return $this->curlPost ( $this->password_server, 'Password=' . $this->password );
	}
	//
	public function actionEncryptPassword() {
		return $this->render ( 'xml', [ 
				'request' => '',
				'response' => $this->getEncryptPassword () 
		] );
	}
	
	//
	public function actionOrderAdd() {
		$xml = $this->getXML ( '0002' );
		
		$body = $xml->getElementsByTagName ( 'Body' ) [0];
		
		$top = $xml->createElement ( 'TypeOfPlan', '' );
		$body->appendChild ( $top );
		$response = $this->curlPost ( $this->api_server, $xml->saveXML () );
		
		return $this->render ( 'xml', [ 
				'request' => $xml->saveXML (),
				'response' => $response 
		] );
	}
	//
	public function xml_policy_create() {
		$xml = $this->getXML ( '0002' );		
		$body = $xml->getElementsByTagName ( 'Body' ) [0];
		
		// $obj=array('insured'=>['1','2','3']);

		// 获取保单的基本信息
		
		// policy
		$policy = $xml->createElement ( 'Policy', '' );
		$policy->appendChild ( $xml->createElement ( 'AgencyPolicyRef', $obj ['policy_id'] ) );
		$policy->appendChild ( $xml->createElement ( 'PlanCode', $obj ['plan_code'] ) );
		$policy->appendChild ( $xml->createElement ( 'IssueDate', $obj ['start_date'] ) );
		$policy->appendChild ( $xml->createElement ( 'EffectiveDate', $obj ['effect_date'] ) );
		$policy->appendChild ( $xml->createElement ( 'ExpireDate', $obj ['expire_date'] ) );
		$policy->appendChild ( $xml->createElement ( 'Destination', $obj ['destination'] ) );
		$policy->appendChild ( $xml->createElement ( 'GroupSize', count ( count($obj ['insured']) ) ) );
		$policy->appendChild ( $xml->createElement ( 'Remark', $obj ['plan_code'] ) );
		$policy->appendChild ( $xml->createElement ( 'PremiumCalType', $obj ['plan_code'] ) );
		$policy->appendChild ( $xml->createElement ( 'TotalPremium', $obj ['plan_code'] ) );
		$body->appendChild ( $policy );
		// agency
		$agency = $xml->createElement ( 'Agency', '' );
		$agency->appendChild ( $xml->createElement ( 'AgencyCode', $this->user_id ) );
		$body->appendChild ( $agency );
		// policy_holder
		$policy_holder = $xml->createElement ( 'PolicyHolder', '' );
		$policy_holder->appendChild ( $xml->createElement ( 'PolicyHolderType', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'PolicyHolderName', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'PHIdType', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'PHIdNumber', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'PHBirthDate', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'PHTelephone', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'PHAddress', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'PHPostCode', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'PHEmail', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'ReqFaPiao', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'ReqMail', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'MailType', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'ReqElecFaPiao', '' ) );
		$policy_holder->appendChild ( $xml->createElement ( 'InvoiceTitle', '' ) );
		
		$body->appendChild ( $policy_holder );
		
		// insured
		$insured_list = $xml->createElement ( 'InsuredList', '' );
		
		foreach ( $obj['insured'] as $m ) {
			$xml_insured = $xml->createElement ( 'Insured', '' );
			$xml_insured->appendChild ( $xml->createElement ( 'InsuredId', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'InsuredName', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'InsuredType', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'IdNumber', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'BirthDate', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'Mobile', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'Email', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'Gender', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'BeneficialType', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'BeneficiaryList', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'EmergencyContact', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'OccupationCode', '' ) );
			$xml_insured->appendChild ( $xml->createElement ( 'PolicyholderInsuredRelation', '' ) );
			$insured_list->appendChild ( $xml_insured );
		}
		
		$body->appendChild ( $insured_list );
		
		return $xml->saveXML ();
	}
	//
	public function actionOrderCancel() {
		$xml = $this->getXML ( '0004' );
		
		$body = $xml->getElementsByTagName ( 'Body' ) [0];
		
		$top = $xml->createElement ( 'TypeOfPlan', '' );
		$body->appendChild ( $top );
		$response = $this->curlPost ( $this->api_server, $xml->saveXML () );
		
		return $this->render ( 'xml', [ 
				'request' => $xml->saveXML (),
				'response' => $response 
		] );
	}
	//
	public function actionOrderSearch() {
		$xml = $this->getXML ( '0005' );
		
		$body = $xml->getElementsByTagName ( 'Body' ) [0];
		
		$top = $xml->createElement ( 'TypeOfPlan', '' );
		$body->appendChild ( $top );
		$response = $this->curlPost ( $this->api_server, $xml->saveXML () );
		
		return $this->render ( 'xml', [ 
				'request' => $xml->saveXML (),
				'response' => $response 
		] );
	}

	/**
	 * 生成支付请求
	 */
	// public function actionOrderPay(){
	// 	$xml = $this->getXML ( '0007' );
	// 	$body = $xml->getElementsByTagName ( 'Body' ) [0];


	// 	$policy_holder = $xml->createElement ( 'PolicyOrder', '' );
	// 	$policy_holder->appendChild ( $xml->createElement ( 'AgencyPolicyRef', '' ) );
	// 	$policy_holder->appendChild ( $xml->createElement ( 'PolicyHolderName', '' ) );
	// 	$policy_holder->appendChild ( $xml->createElement ( 'PHIdType', '' ) );
	// 	$policy_holder->appendChild ( $xml->createElement ( 'PHIdNumber', '' ) );
		
	// }
	
	//
	public function actionOrderPrint() {
		$xml = $this->getXML ( '0006' );
		
		$body = $xml->getElementsByTagName ( 'Body' ) [0];
		
		$top = $xml->createElement ( 'TypeOfPlan', '' );
		$body->appendChild ( $top );
		$response = $this->curlPost ( $this->api_server, $xml->saveXML () );
		
		return $this->render ( 'xml', [ 
				'request' => $xml->saveXML (),
				'response' => $response 
		] );
	}
	//
	public function actionPlanDetail($plan_code) {
		$xml = $this->getXML ( 'PT02' );
		
		$body = $xml->getElementsByTagName ( 'Body' ) [0];
		
		$p1 = $xml->createElement ( 'PlanCode', $plan_code );
		$body->appendChild ( $p1 );
		
		$response = $this->curlPost ( $this->api_server, $xml->saveXML () );
		$x=json_decode ( json_encode ( simplexml_load_string ( $response, 'SimpleXMLElement', LIBXML_NOCDATA ) ), true ) ;
		
		libxml_disable_entity_loader ( true );
		return $this->render ( 'policy_detail', [ 
				'request' => $xml->saveXML (),
				'response' => $response,
				'x'=>$x,
				'plan_code'=>$plan_code
		] );
	}
	//
	public function actionPlanAvailable($type_of_plan) {
		$xml = $this->getXML ( 'PT01' );
		
		$body = $xml->getElementsByTagName ( 'Body' ) [0];
		
		$p1 = $xml->createElement ( 'TypeOfPlan', $type_of_plan );
		$body->appendChild ( $p1 );
		$response = $this->curlPost ( $this->api_server, $xml->saveXML () );
		
		libxml_disable_entity_loader ( true );
		return $this->render ( 'xml', [ 
				'request' => $xml->saveXML (),
				'response' => $response 
		]
		// 'response' => json_decode ( json_encode ( simplexml_load_string ( $response, 'SimpleXMLElement', LIBXML_NOCDATA ) ), true )
		 );
	}

	public function actionTypeOfPlan($top) {
	  $xml = $this->getXML ( 'PT01' );
	  
	  $body = $xml->getElementsByTagName ( 'Body' ) [0];
	  
	  $top = $xml->createElement ( 'TypeOfPlan', $top );
	  $body->appendChild ( $top );
	  $response = $this->curlPost ( $this->api_server, $xml->saveXML () );
	  
	  return $this->render ( 'xml', [ 
	    'request' => $xml->saveXML (),
	    'response' => $response 
	  ]);
	}

	//
	public function actionPlanPrice($plan_code) {
		$xml = $this->getXML ( 'PT03' );
		
		$body = $xml->getElementsByTagName ( 'Body' ) [0];
		
		$body->appendChild ( $xml->createElement ( 'PlanCode', $plan_code ) );
		$body->appendChild ( $xml->createElement ( 'StartDate', '20170210000000' ) );
		$body->appendChild ( $xml->createElement ( 'EndDate', '20170301000000' ) );
		
		$response = $this->curlPost ( $this->api_server, $xml->saveXML () );
		
		libxml_disable_entity_loader ( true );
		return $this->render ( 'xml', [ 
				'request' => $xml->saveXML (),
				'response' => $response 
		] );
	}
	public function xml_policy($param) {
		return "";
	}
}